import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../views/login/login.vue'
// import Home from '../views/home/home'
// import Layout from '../views/layout/layout'
// import Images from '../views/images/image'
// import Content from '../views/contennt/content'
// import Comment from '../views/comment/comment'
// import Article from '../views/article/article'
// import Person from '../views/person/person'
// import Fans from '../views/fans/fans'
// import Edit from '../views/article/edit'
// 下面为路由懒加载引入方式
const Login = () => import('../views/login/login.vue')
const Home = () => import('../views/home/home.vue')
const Layout = () => import('../views/layout/layout.vue')
const Images = () => import('../views/images/image.vue')
const Content = () => import('../views/contennt/content.vue')
const Comment = () => import('../views/comment/comment.vue')
const Article = () => import('../views/article/article.vue')
const Person = () => import('../views/person/person.vue')
const Fans = () => import('../views/fans/fans.vue')
const Edit = () => import('../views/article/edit.vue')
const Page404 = () => import('../views/page404/page404.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  {
    path: '/',
    component: Layout,
    children: [
      { path: '', component: Home, meta: { title: '主页' } },
      { path: '/home', component: Home, meta: { title: '主页' } },
      { path: '/images', component: Images, meta: { title: '素材管理' } },
      { path: '/content', component: Content, meta: { title: '内容管理' } },
      { path: '/comment', component: Comment, meta: { title: '评论管理' } },
      { path: '/article', component: Article, meta: { title: '发布文章' } },
      { path: '/fans', component: Fans, meta: { title: '粉丝管理' } },
      { path: '/person', component: Person, meta: { title: '个人设置' } },
      { path: '/edit/:id', component: Edit, meta: { title: '编辑文章' } }
    ]
  },
  {
    path: '*',
    component: Page404
  }
]

const router = new VueRouter({
  routes
})
const pageList = ['/images', '/article', '/', '/content', '/comment', '/fans', '/person', '/home']
router.beforeEach((to, from, next) => {
  // console.log('系统提示', from, to)
  // next()
  if (to.meta.title) {
    document.title = 'HMPC |' + to.meta.title
  } else {
    document.title = 'hmpc-118'
  }
  const token = localStorage.getItem('token')
  if (pageList.includes(to.path)) {
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else if (to.path === '/login') {
    if (token) {
      next('/')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
